<?php


namespace app\common\model\system;

use think\Model;
use think\model\concern\SoftDelete;


/**
 * @mixin \think\Model
 */
class MaterialLog extends Model
{
    use SoftDelete;

    // 定义时间戳字段名
    protected $createTime = 'create_time';


    //库存修改明细
    public static function MaterialSkuEdit($materialid,$money,$type,$dates,$info)
    {
        if ($money != 0) {
            $material = (new Material())->where('id', $materialid)->find();
            $update = [
                'id' => $material['id'],
                'sku' => $material['sku'] + $money,
            ];
            $res = Material::update($update);
            $res2 = self::create([
                'materialid' => $materialid,
                'befores' => $material['sku'],
                'money' => $money,
                'afters' => $update['sku'],
                'type' => $type,
                'info' => $info,
                'dates' => $dates,
                'adminid' => session('AdminLogin.name') ?? 0
            ]);
            if (!$res || !$res2) {
                throw new \Exception("库存修改失败，请重试", 100);
            }
        }
    }
}
